import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
    state: {
        userInfo: [],
        isLogin: true,
    },
    getters: {
        // 参数列表state指的是state数据
        getUserInfo: state => state.userInfo,
        isLogin: state => state.isLogin,
    },
    mutations: {
        setUser(state, userObj) {
            state.userInfo.push(userObj);
            localStorage.setItem('userinfo', JSON.stringify(userObj));
        },
        userStatus(state, flag) {
            state.isLogin = flag;
            localStorage.setItem("Flag", "isLogin");
        },
    },
    actions: {
        setUserInfo({commit}, userObj) {
            // 跟后台打交道
            // 调用mutaions里面的方法
            commit("setUser", userObj);
        },
        userLogin({commit}, flag) {
            commit("userStatus", flag)
        },
    },
    modules: {}
})
